/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.joe.blockview.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.joe.blockview.ui.BlockView;

import java.lang.ref.WeakReference;

/**
 * Describes a connection on a Block. This can be a previous/next connection, an output, or
 */
public class Connection implements Comparable<Connection> {

    //type -1,0,1,2,3,4区分四种不同的blockview
    private int type = 1;

    public void setType(int t) {
        this.type = t;
    }

    public int getType() {
        return type;
    }


    private WorkspacePoint workspacePoint = new WorkspacePoint();

    private WeakReference<BlockView> blockView;

    public Connection(WeakReference<BlockView> blockView) {
        this.blockView = blockView;
    }

    public BlockView getTagBlockView() {
        return blockView.get();
    }


    //向下连接
    private Connection tagConnection;

    //向上连接
    private Connection preConnection;

    //内部向下连接
    private Connection nextConnection;

    private Connection inputConnection;

    public WorkspacePoint getWorkspacePoint() {
        return workspacePoint;
    }

    public void setWorkspacePoint(WorkspacePoint workspacePoint) {
        this.workspacePoint = workspacePoint;
    }

    /////////////////////////////////
    //判断是否连接可用
    ///////////////////////////////
    private boolean canConnect = true;
    private boolean preCanConnect = true;
    private boolean nextCanConnect = true;
    private boolean inputCanConnect = true;

    public boolean canConnect() {
        return canConnect;
    }

    public void setCanConnect(boolean b) {
        this.canConnect = b;
    }

    public void setPreCanConnect(boolean b) {
        this.preCanConnect = b;
    }

    public boolean getPreCanConnect() {
        return preCanConnect;
    }

    public void setNextConnect(boolean b) {
        this.nextCanConnect = b;
    }

    public boolean getNextConnect() {
        return nextCanConnect;
    }

    public boolean getInputCanConnect() {
        return inputCanConnect;
    }

    public void setInputCanConnect(boolean b) {
        this.inputCanConnect = b;
    }
    /////////////////////////////////////
    /////////////////////////////////////


    /**
     * 目标连接
     *
     * @param tagConnection
     */
    public void connectionTag(Connection tagConnection) {
        this.tagConnection = tagConnection;
    }

    public Connection getTagConnection() {
        return tagConnection;
    }

    public void connectionPre(Connection preConnection) {
        this.preConnection = preConnection;
    }

    public Connection getPreConnection() {
        return preConnection;
    }

    /**
     * 内部向下连接
     *
     * @param connection
     */
    public void connectionNext(Connection connection) {
        this.nextConnection = connection;
    }

    public Connection getNextConnection() {
        return nextConnection;
    }

    public void connectionInput(Connection connection) {
        this.inputConnection = connection;
    }

    public Connection getInputConnection() {
        return inputConnection;
    }

    @Override
    public int compareTo(Connection o) {
        return (int) this.getWorkspacePoint().y - (int) o.getWorkspacePoint().y;
    }

}
